package io.renren.modules.dishes.controller;

import io.minio.Result;
import io.minio.errors.MinioException;
import io.minio.messages.DeleteError;
import io.renren.common.utils.MinioUtilS;
import io.renren.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/minio")
@Slf4j
public class MinioController {

    @Autowired
    private MinioUtilS minioUtilS;
    @Value("${minio.endpoint}")
    private String address;
    @Value("${minio.bucketName}")
    private String bucketName;

    @CrossOrigin
    @PostMapping("/upload")
    public Object upload(MultipartFile file) {
        List<String> upload = minioUtilS.upload(new MultipartFile[]{file});

        return address+""+bucketName+"/"+upload.get(0);
    }

    @CrossOrigin
    @GetMapping("/delete")
    public R delete(@RequestParam Map<String, Object> params){
        String name = (String)params.get("name");
        System.out.println(name);
        String filename = name.substring(name.lastIndexOf('/') + 1);
        minioUtilS.removeFile(bucketName,filename);
        return R.ok();
    }


}


